package cn.net.cfss.fgbp.provider.console.spi;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.net.cfss.fgbp.api.console.constant.DeleteFlagEnum;
import cn.net.cfss.fgbp.api.console.constant.IsLeafNodeEnum;
import cn.net.cfss.fgbp.api.console.constant.OrgTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.net.cfss.fgbp.api.console.dto.SysOrganDto;
import cn.net.cfss.fgbp.api.console.inte.ISysOrganService;
import cn.net.cfss.fgbp.api.console.vo.SysOrganVo;
import cn.net.cfss.fgbp.base.page.PageData;
import cn.net.cfss.fgbp.provider.console.biz.SysOrganDtoBiz;

import javax.transaction.Transactional;

@RestController
@Slf4j
public class SysOrganSpi implements ISysOrganService {

    @Autowired
    SysOrganDtoBiz organDtoBiz;

	@Override
	public PageData<SysOrganVo> pageData(PageData<SysOrganVo> page) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SysOrganVo> list = organDtoBiz.queryList(page.getSearch());
        Page<SysOrganVo> pageInfo = (Page<SysOrganVo>) list;
        page.setRows(list);
        page.setTotal(pageInfo.getTotal());
        return page;
	}

	@Override
	public PageData<SysOrganVo> pageOne(PageData<SysOrganVo> page) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		List<SysOrganVo> list = organDtoBiz.pageOne(page.getSearch());
		Page<SysOrganVo> pageInfo = (Page<SysOrganVo>) list;
		page.setRows(list);
		page.setTotal(pageInfo.getTotal());
		return page;
	}

	@Override
	public List<SysOrganVo> listOne() {
		return organDtoBiz.queryListOne();
	}


	@Override
	public List<SysOrganVo> list(SysOrganVo sysOrganVo) {
		return organDtoBiz.queryList(sysOrganVo);
	}

	@Override
	public List<Long> getIds(Long id) {
		List<Long> ids = new ArrayList<>();
		ids.add(id);
		SysOrganDto sysOrganDto = organDtoBiz.selectById(id);
		SysOrganVo sysOrganVo = new SysOrganVo();
		sysOrganVo.setParentId(id);
		if(OrgTypeEnum.GATHER_ORG.equals(sysOrganDto.getOrganType())){
			List<SysOrganVo> sysOrganVos = organDtoBiz.queryList(sysOrganVo);
			for (SysOrganVo sysOrganVo1 : sysOrganVos){
				ids.add(sysOrganVo1.getId());
				sysOrganVo.setParentId(sysOrganVo1.getId());
				List<SysOrganVo> sysOrganVos1 = organDtoBiz.queryList(sysOrganVo);
				for (SysOrganVo sysOrganVo2 : sysOrganVos1){
					ids.add(sysOrganVo2.getId());
				}
			}
		}
		if(OrgTypeEnum.BRANCH_ORG.equals(sysOrganDto.getOrganType())){
			List<SysOrganVo> sysOrganVos = organDtoBiz.queryList(sysOrganVo);
			for (SysOrganVo sysOrganVo1 : sysOrganVos){
				ids.add(sysOrganVo1.getId());
			}
		}
		return ids;
	}

	@Override
	@Transactional
	public SysOrganDto save(SysOrganDto sysDepartmentDto) {
		sysDepartmentDto.setId(organDtoBiz.getUID());
		sysDepartmentDto.setCreateTime(new Date());
		sysDepartmentDto.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
//		String currMax = organDtoBiz.maxLevel(sysDepartmentDto.getParentId());
//		int len = currMax.length();
//		String level = String.format("%0"+ len +"d", Integer.valueOf(currMax)+1);
//		sysDepartmentDto.setLevel(level);
		log.info("新增机构"+sysDepartmentDto.toString());
		organDtoBiz.insert(sysDepartmentDto);

		//新增机构需要把父级别叶子标识修改
		//把父级别叶子标识设置为否
		Long parentId = sysDepartmentDto.getParentId();
		if(null != parentId && !"".equals(parentId)){
			SysOrganDto dtoParent = new SysOrganDto();
			dtoParent.setId(parentId);
			dtoParent.setIsLeafNode(IsLeafNodeEnum.NOT_LEAF_NODE);
			log.info("修改父级机构标识"+dtoParent.toString());
			organDtoBiz.updateSelectiveById(dtoParent);
		}

		return sysDepartmentDto;
	}

	@Override
	@Transactional
	public SysOrganDto update(SysOrganDto sysDepartmentDto) {
		sysDepartmentDto.setUpdateTime(new Date());
		//更新子节点名称
		organDtoBiz.updateParentId(sysDepartmentDto.getId(),sysDepartmentDto.getName());
		organDtoBiz.updateSelectiveById(sysDepartmentDto);
		return sysDepartmentDto;
	}

	@Override
	public void delete(Long id) {
		organDtoBiz.deleteById(id);
	}

	@Override
	public SysOrganVo detail(Long id) {
		
		return organDtoBiz.getById(id);
	}

	@Override
	public boolean checkAddOrganName(String organName) {
		List<SysOrganVo> sysOrganVoList = organDtoBiz.checkAddOrganName(organName);
		if(null == sysOrganVoList || sysOrganVoList.size() <= 0){
			return false;
		}
		return true;
	}


	@Override
	public boolean checkUpdateOrganName(String organName ,Long id) {
		List<SysOrganVo> sysOrganVoList = organDtoBiz.checkUpdateOrganName(organName,id);
		if(null == sysOrganVoList || sysOrganVoList.size() <= 0){
			return false;
		}
		return true;
	}

	@Override
	public boolean updateParentIsLeafNode(Long id) {
		try {
			//判断同级别下还有没有机构
			SysOrganDto dto = organDtoBiz.selectById(id);
			SysOrganDto sysOrganDto = new SysOrganDto();
			sysOrganDto.setParentId(dto.getParentId());
			sysOrganDto.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
			List<SysOrganDto> sysOrganDtos = organDtoBiz.selectList(sysOrganDto);
			if(null == sysOrganDtos){
				log.error("数据错误！id:{}",id);
				return false;
			}
			//更新父级的是否叶子标识
			if(null != sysOrganDtos && sysOrganDtos.size() <= 1){
				organDtoBiz.updateParentIsLeafNode(dto.getParentId());
			}

		}catch (Exception e){
			log.error("更新父级叶子标识错误！id:{}",id);
			return false;
		}
		return true;
	}

}
